<template>
	<el-dialog title="系统维护" :visible.sync="dialogVisible" width="800px" @close="handleClose"
		:close-on-click-modal="false" append-to-body="true">
		<div slot="title" class="dialog-header-title">
			<i class="el-icon-edit-outline"></i>
			<span>系统维护</span>
		</div>

		<div class="smp-search">
			<el-form :inline="false" label-width="120px" size="mini" ref="form" :model="form" :rules="rules" :disabled="form.requireType=='03'"> 

				<el-row :span="24">
					
						<el-col :span="12">
							<el-form-item label="申请类型:" >
								<el-select v-model="form.requireType" style="width: 100%" filterable remote reserve-keyword  
								placeholder="申请类型" 
								@change="typeChange"
								:disabled="requireTypeDisable">
									<el-option v-for="item in dict('SMP0013')" :key="item.itemCode" :label="item.itemName" :value="item.itemCode"></el-option>
								</el-select>
							</el-form-item>
						</el-col> 
						
						<el-col :span="12">
							<el-form-item label="系统编号:" prop="systemNo">
								<el-input v-model="form.systemNo" placeholder="系统编号" :disabled="systemNoDisabled"></el-input>
							</el-form-item>
						</el-col>
						
						<el-col :span="12">
							<el-form-item label="系统名称:" prop="systemName">
								<el-input v-model="form.systemName" placeholder="系统名称"></el-input>
							</el-form-item>
						</el-col>
						

						<el-col :span="12">
							<el-form-item label="系统类别:" prop="systemType" >
								<!-- <el-input v-model="form.systemType" placeholder="系统类别"></el-input> -->
								<el-select v-model="form.systemType" style="width: 100%" placeholder="系统类别"  @change="systemTypeChange">
									<el-option v-for="item in dict('SMP0014')" :key="item.itemCode" :label="item.itemName" :value="item.itemCode"></el-option>
								</el-select>
							</el-form-item>
						</el-col>

						<el-col :span="12">
							<el-form-item label="所属机构:" prop="systemOrg">
								<el-select :disabled="form.systemType=='03'||form.systemType=='04'"  filterable v-model="form.systemOrg" style="width: 100%" placeholder="所属机构" >
									<!-- <el-option v-for="item in dict('SMP0016')" :key="item.itemCode" :label="item.itemName" :value="item.itemCode"></el-option> -->
									<el-option v-for="item in orgListAll" :key="item.seqCate" :label="item.orgName" :value="item.seqCate"></el-option>
								</el-select>
								
								
								<!-- <el-input v-model="form.systemOrg" placeholder="机构号"></el-input> -->
							</el-form-item>
						</el-col>
						
						<el-col :span="12" v-if="form.systemOrg=='9999'">
							<el-form-item label="第三方名称:" prop="trdName" >
							<el-input v-model="form.trdName" placeholder="第三方名称"></el-input> 
							</el-form-item>
						</el-col>


						<el-col :span="12" >
							<el-form-item label="关联ESB地址:" prop="esbAddr" >
								
								<el-select v-if="esbAddrVisible" v-model="form.esbAddr" style="width: 100%" placeholder="关联ESB地址" >
									<!-- <el-option v-for="item in dict('SMP0016')" :key="item.itemCode" :label="item.itemName" :value="item.itemCode"></el-option> -->
									<el-option v-for="item in esbEnvironmentList" :key="item.ip" :label="item.ip+'('+dictName(item.esbFlg,'SMP0031')+')'" :value="item.ip"></el-option>
								</el-select>
								
							</el-form-item>
						</el-col>
						
						<el-col :span="12">
							<el-form-item label="测试应用IP:" prop="testAddr">
								<el-input v-model="form.testAddr" placeholder="eg:10.0.91.15"></el-input>
							</el-form-item>
						</el-col>
						
						<!-- <el-col :span="12">
							<el-form-item label="生产应用IP:" prop="prodAddr">
								<el-input v-model="form.prodAddr" placeholder="eg:tcp://127.0.0.1:16500"></el-input>
							</el-form-item>
						</el-col> -->
						
						<el-col :span="12">
							<el-form-item label="字符编码:" prop="msgCode">
								
								<el-select v-model="form.msgCode" style="width: 100%" placeholder="字符编码" >
									<el-option v-for="item in dict('SMP0004')" :key="item.itemCode" :label="item.itemName" :value="item.itemCode"></el-option>
								</el-select>
							
							</el-form-item>
						</el-col>
						
						<el-col :span="12">
							<el-form-item label="报文格式:" prop="msgAgreement">
								<el-autocomplete placeholder="请选择或输入报文协议"  style="width: 100%;" :fetch-suggestions="fetchProtocal" v-model="form.msgAgreement" class="inline-input"></el-autocomplete>
							</el-form-item>
						</el-col> 
						
						<el-col :span="12">
							<el-form-item label="系统负责人:" prop="systemResponsibleMan" >
							<el-select disabled v-model="form.systemResponsibleMan" style="width: 100%" filterable remote reserve-keyword
								placeholder="系统负责人" :remote-method="getUsers" >
								<el-option v-for="item in userListAll" :key="item.userName" :label="item.userChineseName"
									:value="item.userName">
									<span style="float: left;margin-left: 3px;">{{ item.userChineseName }}</span>
									<span style="float: right;margin-right: 3px;">{{ item.department }}</span>
								</el-option>
							</el-select>
							
							
							</el-form-item>
						</el-col>
						
						<el-col :span="12">
							<el-form-item label="接入方式:" prop="standardFlg">
								<el-select v-model="form.standardFlg" style="width: 100%" placeholder="接入方式">
									<el-option v-for="item in dict('SMP0030')" :key="item.itemCode" :label="item.itemName" :value="item.itemCode"></el-option>
								</el-select>
							</el-form-item>
						</el-col>
						
				</el-row>
			</el-form>
		</div>
		
		<addSystemView ref="addSystemView" @addSystemView="addSystemView" @resetServer="resetServer"/>
		
		
		<span slot="footer" class="dialog-footer">
			<el-button size="mini" type="primary" @click="onSubmit">提交</el-button>
			<!-- <el-button size="mini" @click="reset">清空</el-button> -->
			<el-button size="mini" @click="closeDialog">取消</el-button>
		</span>
	</el-dialog>
</template>

<script>
	"use strict";
	import addSystemView from './addSystemView';
	import {getRequirementList} from '@/api/demand.js';
	import {getSystemCheck} from '@/api/demand.js';
	import {getEsbEnvironmentByEsbFlg} from '@/api/environment.js';
	
	export default {
		components: {
			addSystemView
		},
		data() {
			return {
				index:0,
				type:'add',
				close:'0',
				esbEnvironmentList: [],
				dialogVisible: false,
				esbAddrVisible:true,
				requireTypeDisable:false,
				systemNoDisabled:false,
				 rules: {
					systemNo: [{
						required: true,
						message: "系统编号不能为空",
						trigger: ['blur', 'change']
					}],
					systemName: [{
						required: true,
						message: "系统名称不能为空",
						trigger: ['blur', 'change']
					}],
					systemResponsibleMan: [{
						required: true,
						message: "负责人不能为空",
						trigger: ['blur', 'change']
					}],
					testAddr: [{
						required: true,
						message: "测试应用IP不能为空",
						trigger: ['blur', 'change']
					}],
					esbAddr: [{
						required: true,
						message: "ESB测试IP不能为空",
						trigger: ['blur', 'change']
					}],
					msgAgreement: [{
						required: true,
						message: "报文格式不能为空",
						trigger: ['blur', 'change']
					}],
					msgCode: [{
						required: true,
						message: "报文编码不能为空",
						trigger: ['blur', 'change']
					}],
					systemType: [{
						required: true,
						message: "系统类型不能为空",
						trigger: ['blur', 'change']
					}],
					standardFlg: [{
						required: true,
						message: "接入方式不能为空",
						trigger: ['blur', 'change']
					}],
					trdName: [{
						required: true,
						message: "请输入第三方名称",
						trigger: ['blur', 'change']
					}],
					
					
				}, 
				form: {
					 applyId:'',
					 systemNo: '',
					 systemName: '',
					 systemOrg: '',
					 testAddr: '',
					 prodAddr: '',
					 esbAddr: '',
					 systemType: '',
					 requireType: '',
					 msgCode:'',
					 msgAgreement:'',
					 systemResponsibleMan:'',
					 standardFlg:'',
					 trdName:''
				},

			};
		},
		
		created() {
			this.getEsbEnvironmentList();
		},

		
		methods: {
			systemTypeChange(){
				if(this.form.systemType=='03'||this.form.systemType=='04'){
					this.form.systemOrg='9999';
				}else{
					this.form.systemOrg='';
				}
				
				this.getEsbEnvironmentList();
			},
			
			/* getEsbEnvironmentList() {
				getEsbEnvironmentByEsbFlg({}).then(response => {
					this.esbEnvironmentList = response.data.extend;
				}) */
				
			getEsbEnvironmentList() {
				//this.form.esbAddr = null;
				 /* let system = this.$store.getters.getSystem(this.form.systemNo); */
				 let systemType= this.form.systemType=='01'?'01':'02';
				this.esbAddrVisible = false;
				getEsbEnvironmentByEsbFlg({esbFlg:systemType}).then(response => {
					this.esbEnvironmentList = response.data.extend
					let  lists =  this.esbEnvironmentList.filter(item=>item.ip == this.form.esbAddr)
					if(lists.length ==0){
						this.form.esbAddr = ""
					}
					this.$nextTick(()=>{
						//this.form.esbAddr = this.esbEnvironmentList[0].i
						this.esbAddrVisible = true;
					});
					//this.dialogVisible = true;
				})
			},
						
			fetchProtocal(queryString,cb){
			var protocals = [{value:"socket/xml"},{value:"http/json"},{value:"http/soap"},{value:"https/json"},{value:"定长"},{value:"分隔符"},{value:"其他"}];
				
				var protocals1 =
				protocals1  = queryString? protocals.filter(item=>{
					
					return item.value.indexOf(queryString)>-1;
				}):protocals;
				cb(protocals1);
			},
			

			onSubmit() {
				this.$refs["form"].validate(valid => {
					if (valid) {
						
						let data = {};
						Object.assign(data,this.form);
						//校验
						getSystemCheck({"systemNo":data.systemNo,"applyId":data.applyId,"requireType":data.requireType}).then(response => {
							if(response.data.code=='0000'){
								
								if(this.type=='add'){
									this.$emit('addApplySystem',data);
								}else{
									this.$emit('updateApplySystem',data,this.index);
								}
								this.dialogVisible = false;
								this.$refs["form"].resetFields();
							}else{
								this.$message.error(response.data.message);
							}
						}).catch(error => {
							console.info("失败");
							console.info(error.message);
						});
						
						
					}else{
						return false;
					}
				});
				

			},

			reset() {
				this.$refs["form"].resetFields();
			},

			// systemRegister() {
			// 	this.$refs["form"].validate(valid => {
			// 		if (valid) {
			// 			addSystemInfo(this.form).then(response => {
			// 				this.data = response.data.extend;
			// 				this.$message({
						//duration:5000,
			// 					type: 'success',
			// 					message: '系统登记成功！'
			// 				});
			// 				this.form = {};
			// 				this.$refs["form"].resetFields();
			// 				this.dialogVisible = false;
			// 				this.$emit("onSubmit");
			// 			}).catch(error => {
			// 				this.$message({
						//duration:5000,			// 					type: 'error',
			// 					message: '系统登记失败！'
			// 				});
			// 				this.form = {};
			// 				this.$refs["form"].resetFields();
			// 				this.dialogVisible = false;
			// 				this.$emit("onSubmit");
			// 			});
			// 		}
			// 	})

			// },

			showDialog(applyId) {
				this.type='add';	
				this.form.systemResponsibleMan = this.$store.getters.getActiveUser().userName;
				this.form.requireType="01";
				this.form.applyId=applyId;
				this.dialogVisible = true;
				this.requireTypeDisable = false;
				this.systemNoDisabled=false;
			},
			
			showDialog2(row,index) {
				this.type='update';
				this.index = index;
				this.form = row;
				this.dialogVisible = true;
				this.requireTypeDisable = true;
				this.systemNoDisabled=true;
				
			},
			
			closeDialog() {
				this.$refs["form"].resetFields();
				this.dialogVisible = false;
			},

			handleClose() {
				this.dialogVisible = false;
			},
			
		
			typeChange(val){
				this.close='0';
				if(val=="01"){
					this.reset();
				}else{ //选择修改删除的时候弹窗
					this.$refs.addSystemView.showDialog();
					this.requireTypeDisable = true;
				}
			},
			
			addSystemView(row){
				this.close='1';
				Object.assign(this.form,row);
				this.getEsbEnvironmentList();
				

			},
			
			resetServer(){
				if(this.close=="0"){
					this.type='add';
					this.form.systemResponsibleMan = this.$store.getters.getActiveUser().userName;
					this.form.requireType="01";
					this.dialogVisible = true;
					this.requireTypeDisable = false;
					this.systemNoDisabled=false;
				}
				
			}


		},

	}
</script>

<style>
</style>
